package com.rzico.sbl.ui.manager

import androidx.core.view.isVisible
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityTaxDetailBinding
import com.rzico.sbl.model.BaseUrl
import com.rzico.sbl.model.TaxData
import com.rzico.sbl.ui.member.MemberDetailActivity
import com.rzico.sbl.viewmodel.CommonViewModel
import com.xinnuo.common.extend.*
import com.xinnuo.common.helper.copyStringToClip
import com.xinnuo.common.helper.getTimeString
import com.xinnuo.common_ui.base.BaseActivity
import org.json.JSONObject

class TaxDetailActivity : BaseActivity(R.layout.activity_tax_detail) {

    private val mBinding by lazy { ActivityTaxDetailBinding.bind(getRootView()) }

    private var mCardId = ""
    private var mMemberId = ""

    override fun getViewModel() = get<CommonViewModel>()

    override fun initData() {
        initTitle(
            title = intent.getExtra("title"),
            name = "一键复制"
        )

        initDisplay(intent.getParcelableExtra("data"))
    }

    private fun initDisplay(item: TaxData?) {
        item?.let {
            mBinding.apply {
                riseLayout.isVisible = it.type == "special"

                taxTime.text = it.createDate.getTimeString()
                taxCode.text = it.taxId.uppercase()
                taxCompany.text = it.name
                taxAmount.text = it.amount
                taxMemo.text = it.memo
                riseAccount.text = it.account
                riseBank.text = it.bank
                riseAdress.text = it.address
                risePhone.text = it.phone
            }

            mCardId = it.cardId
            mMemberId = it.memberId

            getMemberName(it.cardId)
        }

        tvRight?.oneClick {
            item?.let {
                mBinding.apply {
                    val hintList = arrayListOf<String>()

                    taxName.content
                        .notEmpty { hintList += "申请人：${this}" }
                    taxTime.content
                        .notEmpty { hintList += "申请时间：${this}" }
                    taxCode.content
                        .notEmpty { hintList += "纳税人识别号：${this}" }
                    taxCompany.content
                        .notEmpty { hintList += "公司名称：${this}" }

                    (it.type == "special").condition {
                        riseAdress.content
                            .notEmpty { hintList += "公司地址：${this}" }
                        risePhone.content
                            .notEmpty { hintList += "公司电话：${this}" }
                        riseAccount.content
                            .notEmpty { hintList += "账户：${this}" }
                        riseBank.content
                            .notEmpty { hintList += "开户行：${this}" }
                    }

                    taxAmount.content
                        .notEmpty { hintList += "金额：${this}" }
                    taxMemo.content
                        .notEmpty { hintList += "备注：${this}" }

                    copyStringToClip(
                        hintList.joinToString("\n")
                    )

                    showToast("发票信息复制成功！")
                }
            }
        }
    }

    override fun initListener() {
        mBinding
            .apply {
                taxNameLl.oneClick {
                    (mCardId.isNotEmpty() && mMemberId.isNotEmpty())
                        .condition {
                            startActivityEx<MemberDetailActivity>(
                                "cardId" to mCardId,
                                "memberId" to mMemberId,
                            )
                        }
                }

                taxNameLl.longClick {
                    taxName.content
                        .notEmpty {
                            copyStringToClip(this)

                            showToast("申请人复制成功！")
                        }
                }

                taxTimeLl.longClick {
                    taxTime.content
                        .notEmpty {
                            copyStringToClip(this)

                            showToast("申请时间复制成功！")
                        }
                }

                taxCodeLl.longClick {
                    taxCode.content
                        .notEmpty {
                            copyStringToClip(this)

                            showToast("纳税人识别号复制成功！")
                        }
                }

                taxCompanyLl.longClick {
                    taxCompany.content
                        .notEmpty {
                            copyStringToClip(this)

                            showToast("公司名称复制成功！")
                        }
                }

                taxAmountLl.longClick {
                    taxAmount.content
                        .notEmpty {
                            copyStringToClip(this)

                            showToast("金额复制成功！")
                        }
                }

                riseAccountLl.longClick {
                    riseAccount.content
                        .notEmpty {
                            copyStringToClip(this)

                            showToast("账户复制成功！")
                        }
                }

                riseBankLl.longClick {
                    riseBank.content
                        .notEmpty {
                            copyStringToClip(this)

                            showToast("开户行复制成功！")
                        }
                }

                riseAdressLl.longClick {
                    riseAdress.content
                        .notEmpty {
                            copyStringToClip(this)

                            showToast("公司地址复制成功！")
                        }
                }

                risePhoneLl.longClick {
                    risePhone.content
                        .notEmpty {
                            copyStringToClip(this)

                            showToast("公司电话复制成功！")
                        }
                }

                taxMemoLl.longClick {
                    taxMemo.content
                        .notEmpty {
                            copyStringToClip(this)

                            showToast("备注复制成功！")
                        }
                }
            }
    }

    /**
     * 会员详情
     */
    private fun getMemberName(cardId: String) {
        getViewModel()
            .request(
                requestUrl = BaseUrl.memberInfo,
                params = arrayOf("id" to cardId),
                onSuccess = {
                    val objOuter = JSONObject(it)
                    val obj = objOuter.optJSONObject("card") ?: JSONObject()

                    mCardId = obj.optNotNull("id")
                    mMemberId = obj.optNotNull("memberId")

                    mBinding.taxName.text = obj.optNotNull("name")
                }
            )
    }

}